package cn.cordys.crm.system.controller;

import cn.cordys.common.constants.PermissionConstants;
import cn.cordys.common.dto.DeptUserTreeNode;
import cn.cordys.common.dto.OptionDTO;
import cn.cordys.common.pager.PageUtils;
import cn.cordys.common.pager.Pager;
import cn.cordys.context.OrganizationContext;
import cn.cordys.crm.system.dto.request.*;
import cn.cordys.crm.system.dto.response.RoleListResponse;
import cn.cordys.crm.system.dto.response.UserImportResponse;
import cn.cordys.crm.system.dto.response.UserPageResponse;
import cn.cordys.crm.system.dto.response.UserResponse;
import cn.cordys.crm.system.service.OrganizationConfigService;
import cn.cordys.crm.system.service.OrganizationUserService;
import cn.cordys.crm.system.service.RoleService;
import cn.cordys.security.SessionUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@RestController
@RequestMapping("/user")
@Tag(name = "用户(员工)")
public class OrganizationUserController {

    @Resource
    private OrganizationUserService organizationUserService;
    @Resource
    private RoleService roleService;
    @Resource
    private OrganizationConfigService organizationConfigService;

    @PostMapping("/list")
    @Operation(summary = "用户(员工)-列表查询")
    @RequiresPermissions(PermissionConstants.SYS_ORGANIZATION_READ)
    public Pager<List<UserPageResponse>> list(@Validated @RequestBody UserPageRequest request) {
        Page<Object> page = PageHelper.startPage(request.getCurrent(), request.getPageSize());
        return PageUtils.setPageInfo(page, organizationUserService.list(request));
    }


    @PostMapping("/add")
    @RequiresPermissions(PermissionConstants.SYS_ORGANIZATION_ADD)
    @Operation(summary = "用户(员工)-添加员工")
    public void addUser(@Validated @RequestBody UserAddRequest request) {
        organizationUserService.addUser(request, OrganizationContext.getOrganizationId(), SessionUtils.getUserId());
    }


    @GetMapping("/detail/{id}")
    @Operation(summary = "用户(员工)-员工详情")
    @RequiresPermissions(PermissionConstants.SYS_ORGANIZATION_READ)
    public UserResponse getUserDetail(@PathVariable String id) {
        return organizationUserService.getUserDetail(id);
    }

    @PostMapping("/update")
    @RequiresPermissions(PermissionConstants.SYS_ORGANIZATION_UPDATE)
    @Operation(summary = "用户(员工)-更新")
    public void updateUser(@Validated @RequestBody UserUpdateRequest request) {
        organizationUserService.updateUser(request, SessionUtils.getUserId(), OrganizationContext.getOrganizationId());
    }


    @GetMapping("/reset-password/{userId}")
    @RequiresPermissions(PermissionConstants.SYS_ORGANIZATION_USER_RESET_PASSWORD)
    @Operation(summary = "用户(员工)-重置密码")
    public void resetPassword(@PathVariable String userId) {
        organizationUserService.resetPassword(userId, SessionUtils.getUserId(), OrganizationContext.getOrganizationId());
    }


    @PostMapping("/batch-enable")
    @RequiresPermissions(PermissionConstants.SYS_ORGANIZATION_UPDATE)
    @Operation(summary = "用户(员工)-批量启用/禁用")
    public void enable(@Validated @RequestBody UserBatchEnableRequest request) {
        organizationUserService.enable(request, SessionUtils.getUserId(), OrganizationContext.getOrganizationId());
    }

    @PostMapping("/batch/reset-password")
    @RequiresPermissions(PermissionConstants.SYS_ORGANIZATION_USER_RESET_PASSWORD)
    @Operation(summary = "用户(员工)-批量重置密码")
    public void batchResetPassword(@Validated @RequestBody UserBatchRequest request) {
        organizationUserService.batchResetPassword(request, SessionUtils.getUserId(), OrganizationContext.getOrganizationId());
    }

    @PostMapping("/update/name")
    @Operation(summary = "用户(员工)-更新名称")
    @RequiresPermissions(PermissionConstants.SYS_ORGANIZATION_UPDATE)
    public void updateName(@Validated @RequestBody UserUpdateName request) {
        organizationUserService.updateUserName(request, SessionUtils.getUserId());
    }


    @PostMapping("/batch/edit")
    @Operation(summary = "用户(员工)-批量编辑")
    @RequiresPermissions(PermissionConstants.SYS_ORGANIZATION_UPDATE)
    public void batchEditUser(@Validated @RequestBody UserBatchEditRequest request) {
        organizationUserService.batchEditUser(request, SessionUtils.getUserId(), OrganizationContext.getOrganizationId());
    }


    @GetMapping("/download/template")
    @Operation(summary = "用户(员工)-excel导入-下载模板")
    @RequiresPermissions(PermissionConstants.SYS_ORGANIZATION_ADD)
    public void userTemplateExport(HttpServletResponse response) {
        organizationUserService.downloadExcelTemplate(response);
    }


    @PostMapping("/import/pre-check")
    @Operation(summary = "用户(员工)-excel导入检查")
    @RequiresPermissions(PermissionConstants.SYS_ORGANIZATION_IMPORT)
    public UserImportResponse preCheckXMind(@RequestPart(value = "file", required = false) MultipartFile file) {
        return organizationUserService.preCheck(file, OrganizationContext.getOrganizationId());
    }


    @PostMapping(value = "/import")
    @Operation(summary = "用户(员工)-excel导入")
    @RequiresPermissions(PermissionConstants.SYS_ORGANIZATION_IMPORT)
    public UserImportResponse importUser(@RequestPart(value = "file", required = false) MultipartFile file) {
        return organizationUserService.importByExcel(file, SessionUtils.getUserId(), OrganizationContext.getOrganizationId());
    }


    @GetMapping(value = "/option")
    @Operation(summary = "获取用户下拉option")
    public List<OptionDTO> getUserList() {
        return organizationUserService.getUserOptions(OrganizationContext.getOrganizationId());
    }

    @GetMapping(value = "/role/option")
    @Operation(summary = "获取用户角色下拉option")
    @RequiresPermissions(PermissionConstants.SYS_ORGANIZATION_READ)
    public List<OptionDTO> getUserRoleList() {
        List<RoleListResponse> list = roleService.list(OrganizationContext.getOrganizationId());
        return list.stream().map(role -> new OptionDTO(role.getId(), role.getName())).toList();
    }


    @GetMapping("/sync-check")
    @Operation(summary = "用户(员工)-是否为第三方同步数据")
    @RequiresPermissions(PermissionConstants.SYS_ORGANIZATION_READ)
    public boolean syncCheck() {
        return organizationConfigService.syncCheck(OrganizationContext.getOrganizationId());
    }


    @GetMapping("/delete/{id}")
    @RequiresPermissions(PermissionConstants.SYS_ORGANIZATION_DELETE)
    @Operation(summary = "用户(员工)-删除")
    public void deleteUser(@PathVariable String id) {
        organizationUserService.deleteUserById(id, OrganizationContext.getOrganizationId());
    }


    @GetMapping("/delete/check/{id}")
    @RequiresPermissions(PermissionConstants.SYS_ORGANIZATION_DELETE)
    @Operation(summary = "用户(员工)-删除校验")
    public boolean deleteCheck(@PathVariable String id) {
        return organizationUserService.deleteCheck(id);
    }


    @GetMapping("/get/{departmentId}")
    @Operation(summary = "获取指定部门下用户")
    @RequiresPermissions(PermissionConstants.SYS_ORGANIZATION_READ)
    public List<DeptUserTreeNode> getDepartmentUser(@PathVariable String departmentId) {
        return organizationUserService.getUserTreeByDepId(departmentId, OrganizationContext.getOrganizationId());
    }
}
